<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal" />
      </el-col>

      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="display: flex;justify-content: space-between;">
          <div>
            <el-button style="margin: 3px 0 0 0;" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
            <el-form-item label="拌合站" prop="identifier">
              <el-select  style="width:130px"  v-model="queryParams.identifier" clearable placeholder="请选择">
                <el-option v-for="item in stationList" :key="item.name" :label="item.name" :value="item.identifier"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="报警时间" prop="insterTime" > 
              <el-date-picker  style="width:130px" clearable v-model="queryParams.insterTime" type="date" value-format="yyyy-MM-dd" placeholder="报警时间"></el-date-picker>
            </el-form-item>

            <el-form-item label="处置状态" prop="disposalStatus">
              <el-select  style="width:120px" v-model="queryParams.disposalStatus" clearable size="mini" placeholder="请选择">
                <el-option v-for="item in dict.type.warning_disposal_status" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="报警等级" prop="warningGrade">
              <el-select style="width:120px" v-model="queryParams.warningGrade" clearable size="mini" placeholder="请选择">
                <el-option v-for="item in warningGradeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['zhgd:nWarningHnt:query']">搜索</el-button>
            </el-form-item>
          </div>

          <el-form-item>
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                       @click="handleDelete" v-hasPermi="['zhgd:nWarningHnt:remove']">删除</el-button>
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['zhgd:nWarningHnt:export']">导出</el-button>
          </el-form-item>
        </el-form>

        <el-table border v-loading="loading" :data="nWarningHntList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="40" align="center" />
          <el-table-column label="拌合站" align="center" prop="mixingStation" min-width="130" show-overflow-tooltip/>
          <el-table-column label="项目名称" align="center" prop="projName" min-width="110" show-overflow-tooltip/>
          <el-table-column label="施工部位" align="center" prop="consPos" min-width="80" show-overflow-tooltip/>
          <el-table-column label="配比名称" align="center" prop="recipe" min-width="65" show-overflow-tooltip/>
          <el-table-column label="盘序号" align="center" prop="serial" width="60" show-overflow-tooltip/>
          <el-table-column label="报警类型" align="center" prop="type" width="110" show-overflow-tooltip/>
          <el-table-column label="报警时间" align="center" prop="insterTime" width="135">
            <template slot-scope="scope"><span>{{ parseTime(scope.row.insterTime) }}</span></template>
          </el-table-column>
          <el-table-column label="拌合站编号" align="center" prop="stationCode" width="120" show-overflow-tooltip/>
          <el-table-column label="报警等级" align="center" prop="warningGrade" width="75"/>
          <el-table-column label="超出上下限" align="center" prop="error" width="90" show-overflow-tooltip/>
          <el-table-column label="处置状态" align="center" prop="disposalStatus" width="80" />
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
            <template slot-scope="scope">
              <!-- <el-button size="mini" :key="1" type="text" :disabled="scope.row.disposalStatus != '未处置'" @click="addDisposal(scope.row.id)">处置</el-button> -->
              <el-button size="mini" :key="1" type="text" :disabled="handleStatus(scope.row.disposalStatus)" @click="addDisposal(scope.row.id)" v-hasPermi="['zhgd:nWarningHnt:handle']">处置</el-button>
              <el-button size="mini" :key="2" type="text" :disabled="scope.row.disposalStatus != '处置中'" @click="ensureDisposal(scope.row.id)" v-hasPermi="['zhgd:nWarningHnt:confirm']">确认</el-button>
              <el-button size="mini" :key="3" type="text" :disabled="scope.row.disposalStatus != '处置中'" @click="withdraw(scope.row.id)" v-hasPermi="['zhgd:nWarningHnt:withdraw']">撤回</el-button>
              <el-button size="mini" :key="4" type="text" :disabled="scope.row.disposalStatus == null || scope.row.disposalStatus == '未处置'" @click="handleDisposalDetail(scope.row.id)" v-hasPermi="['zhgd:nWarningHnt:details']">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--处置单dialog-->
        <Disposal v-if="open" :open="open" :warningId = "warningId" ref="disposalWarning" :warningType = "4" @getList='getList'  @closeDialog = 'closeDialog'/>
        <!--确认单dialog-->
        <DisposalConfirm ref="disposalConfirm" :open="confirmVisible" @getList='getList'/>
        <!--详情dialog-->
        <DisposalConfirm ref="disposalDetail" :open="detalVisible" @getList='getList'/>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listNWarningHnt, getNWarningHnt, delNWarningHnt, addNWarningHnt, updateNWarningHnt,handleRevoke } from "@/api/zhgd/nWarningHnt";
import { deptListData } from "@/api/zhgd/hntManage";
import workSiteLeft from "@/views/components/workSiteLeft";
import Disposal from './disposal.vue';
import DisposalConfirm from './disposalConfirm.vue';


export default {
  name: "NWarningHnt",
  components: {
    workSiteLeft,
    Disposal,
    DisposalConfirm,
  },
  dicts: ['warning_disposal_status'],
  data() {
    return {
      warningGradeList:[{label:'初级',value:'初级'},{label:'中级',value:'中级'},{label:'高级',value:'高级'}],
      disposalStatus:this.$route.query.disposalStatus,
      confirmVisible:false,
      detalVisible:false,
      warningId:'',
      identifier: "",
      show: true,
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      pk_id: "",
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自建混凝土拌合站报警管理表格数据
      nWarningHntList: [],
      // 拌合站搜索框数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        insterTime: undefined,
        mixingStation: undefined,
        projName: undefined,
        consPos: undefined,
        recipe: undefined,
        stationCode: undefined,
        warningGrade: undefined,
        disposalStatus:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "主键不能为空", trigger: "blur" }],
        type: [{ required: true, message: "报警类型不能为空", trigger: "change" }],
        insterTime: [{ required: true, message: "报警时间不能为空", trigger: "blur" }],
        mixingStation: [{ required: true, message: "拌合站不能为空", trigger: "blur" }],
        message: [{ required: true, message: "报警信息不能为空", trigger: "blur" }],
        stationCode: [{ required: true, message: "拌合站编号不能为空", trigger: "blur" }],
        warningGrade: [{ required: true, message: "报警等级不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.queryParams.disposalStatus = this.disposalStatus;
    this.getList();
    this.getProjectList();
  },
  methods: {
    // selectChange(e){
    //   this.queryParams.disposalStatus = e;
    // },
    //详情
    handleDisposalDetail(id){
      this.detalVisible=true;
      this.$refs.disposalDetail.dialogOpen = true;
      this.$refs.disposalDetail.title = "混凝土拌合站预警处置详情"
      this.$refs.disposalDetail.disabled = true
      this.$refs.disposalDetail.detailsDisabled = true
      this.$refs.disposalDetail.form = {"warningId":id,"warningType":"4"}
      this.$refs.disposalDetail.details()
    },
    handleStatus(status){
      if(status == '未处置' || status == '驳回' || status == '草稿'){
        return false;
      }
      return true;
    },
    //撤回按钮
    withdraw(id){
      this.$confirm('确认撤回？')
        .then(_ => {
          let params={
            warningType:"4",
            warningId:id,
            status:2,
            operation:"1",
          }
          handleRevoke(params).then(res=>{
            this.$message.success("撤回成功")
            this.getList()
          })
          // 调用撤回接口
        })
        .catch(_ => {});
    },
    //确认按钮
    ensureDisposal(id){
      this.confirmVisible=true;
      this.$refs.disposalConfirm.dialogOpen = true;
      this.$refs.disposalConfirm.title = "混凝土拌合站预警处置确认"
      this.$refs.disposalConfirm.disabled = true
      this.$refs.disposalConfirm.detailsDisabled = false
      this.$refs.disposalConfirm.form = {"warningId":id,"warningType":"4"}
      this.$refs.disposalConfirm.getInfoByWarningId()
    },
    closeDialog(){
      this.open = false;
    },
    //新增处置
    addDisposal(id){
      this.reset();
      this.open = true;
      this.warningId = id;
      this.title = "添加预警处置信息";
      this.$nextTick(() => {
        this.$refs.disposalWarning.title = "添加预警处置信息";
        this.$refs.disposalWarning.form = {"warningId":id,"warningType":"4"}
        this.$refs.disposalWarning.getInfoByWarningId()
      })
    },
    /** 查询自建混凝土拌合站报警管理列表 */
    getList() {
      this.loading = true;
      listNWarningHnt(this.queryParams).then(response => {
        this.nWarningHntList = response.rows.map(e=>{
          this.dict.type.warning_disposal_status.forEach(item=>{
            if(item.value == e.disposalStatus){
              e.disposalStatus = item.label
            }
          })
          return e;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询拌合站列表 */
    getProjectList() {
      deptListData(this.queryParams).then((response) => {
        this.stationList = response;
      });
    },
    // 左侧树点击事件
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      this.queryParams.id = val.id;
      this.handleQuery();
      this.getProjectList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        insterTime: undefined,
        mixingStation: undefined,
        message: undefined,
        stationCode: undefined,
        warningGrade: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.workSiteLeft.resetQuery();
      this.queryParams.id = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自建混凝土拌合站报警管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getNWarningHnt(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改自建混凝土拌合站报警管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateNWarningHnt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addNWarningHnt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(() => {
        this.loading = true;
        return delNWarningHnt(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhgd/nWarningHnt/export', {
        ...this.queryParams
      }, `nWarningHnt_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .query {
  .el-form-item__label {
    width: 80px !important;
  }
}
</style>
